package cn.gailvlun.gll.presentation.apartment.task;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.github.markzhai.recyclerview.BaseViewAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.databinding.ActivityTaskSettingBinding;
import cn.gailvlun.gll.db.TaskDb;
import cn.gailvlun.gll.net.apartment.room.Task;
import cn.gailvlun.gll.presentation.apartment.task.adapter.TaskSettingAdapter;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.StringUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class TaskSettingActivity extends BaseActivity {

    public static void openActivity(Activity context, int hours, ArrayList<Integer> ids, Consumer<Result<Activity>> consumer) {
        Intent intent = new Intent(context, TaskSettingActivity.class);
        intent.putExtra("hours", hours);
        intent.putIntegerArrayListExtra("idList", ids);
        RxActivityResult.on(context)
                .startIntent(intent)
                .subscribe(consumer);
    }

    private ActivityTaskSettingBinding mBinding;
    private int mHours; 

    private TaskSettingAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_task_setting);

        mAdapter = new TaskSettingAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.rv.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(mLayoutManager);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.nbv.setOnMenuClickListener(new NavigationBarView.OnMenuClickListener() {
            @Override
            public void onMenuClick(NavigationBarView nb) {
                
                if (mAdapter.isComplete()) {
                    Intent intent = new Intent();
                    intent.putIntegerArrayListExtra("idList", mAdapter.getIdList());
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    ToastUtil.showLong("请将所有的任务填满");
                }
            }
        });
    }

    private ArrayList<Integer> idList;

    public static int getTaskNumberByHours(int hour) {
        if (hour < 24) {
            return 1;
        }
        return hour / 24;
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        mHours = getIntent().getIntExtra("hours", 1);
        idList = getIntent().getIntegerArrayListExtra("idList");

        mAdapter.clear();

        if (!IsEmpty.list(idList)) {
            
            List<Task> tasks = TaskDb.getInstance().getTasksById(idList);
            for (int i = 0; i < getTaskNumberByHours(mHours); i++) {
                Task task = new Task();
                task.setSequence_num(i + 1);
                task.setTitle(tasks.get(i).getTitle());
                task.setDesc(tasks.get(i).getDesc());
                task.setId(tasks.get(i).getId());
                mAdapter.add(task);
            }
        } else {
            for (int i = 0; i < getTaskNumberByHours(mHours); i++) {
                Task task = new Task();
                task.setSequence_num(i + 1);
                mAdapter.add(task);
            }
        }
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        public void onSelectDay(final Task item) {
            TaskLibraryActivity.openActivity(mContext, mAdapter.getIdList(), new Consumer<Result<Activity>>() {
                @Override
                public void accept(Result<Activity> activityResult) throws Exception {
                    if (activityResult.data() != null) {

                        int taskId = activityResult.data().getIntExtra("taskId", -1);
                        if (taskId != -1) {
                            Task task = TaskDb.getInstance().getTaskById(taskId);
                            
                            if (mAdapter.indexId(task.getId()) != -1) {
                                Task old = mAdapter.getData().get(mAdapter.indexId(task.getId()));
                                old.setId(null);
                                old.setTitle(null);
                                old.setDesc(null);
                            }

                            item.setId(task.getId());
                            item.setTitle(task.getTitle());
                            item.setDesc(task.getDesc());
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }

    public static class Util {
        public static String getDay(Task item) {
            return "第" + StringUtil.num2Chn(item.getSequence_num()) + "天";
        }
    }
}
